<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '154px'
    },
    chartData: {
      type: Object,
      // required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ value, name } = {}) {

      this.chart.setOption({
        title: {
            text: '',
            x: 'center',
            textStyle: {
              color: '#000',
              fontSize: 12,
              align: 'right',
            }
        },
        series: [{
          name: '',
          textStyle: {
            color: '#fff'
          },
          type: 'gauge',
          startAngle: 200,
          endAngle: -20,
          //仪表盘详情，用于显示数据。
          // 刻度
          splitNumber: 5,
          min: 0,
          max: 100,
          radius:'95%',
          center: ['50%', '55%'],
          axisLine: { // 坐标轴线
            lineStyle: { // 属性lineStyle控制线条样式
              color: [
                [0.8, '#3BC27F'],
                [0.9, '#FAB120'],
                [1.0, '#E96C5B']
              ],
              width: 5
            }
          },
          axisTick: { // 坐标轴小标记
            show: true, // 属性show控制显示与否，默认不显示
            splitNumber: 5, // 每份split细分多少段
            length: 5, // 属性length控制线长
            distance: 3, // 与轴线的距离
            lineStyle: { // 属性lineStyle控制线条样式
              color: '#000',
              width: 1,
              type: 'solid'
            }
          },
          axisLabel: { // 刻度标签
            distance: 7, // 标签与刻度线的距离
          },
          splitLine: { // 分隔线
            length: 10, // 属性length控制线长
            distance: 3, // 分隔线与轴线的距离
            lineStyle: { // 属性lineStyle（详见lineStyle）控制线条样式
              color: '#000'
            }
          },
          pointer: {
            width: 5,
            length: '50%',
            itemStyle: {
              color: "auto"
            }
          },
          anchor: {
            show: true,
            showAbove: true,
            size: 5,
            itemStyle: {
              color: "#FFFFFF",
              // borderColor: "auto",
              borderWidth: 10
            }
          },
          detail: {
            show: true,
            offsetCenter: [0, '50%'],
            textStyle: {
              fontSize: 24,
              color: 'auto',
            }
          },
          title: {
            offsetCenter: ["0", "75%"],
            fontSize: 14
          },
          data: [{
            value: value,
            name: name
          }]
        }]
      })
    }
  }
}
</script>
